package com.alen.sun.controller;

import com.alen.sun.bean.Activity;
import com.alen.sun.bean.Folk;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.FolkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/activities")
@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result addActivity(@RequestBody Activity activity) {
        Boolean flag = activityService.addActivity(activity);
        if(flag){
            return Result.success();
        }else {
            return Result.error("添加活动失败");
        }
    }

    /**
     * 修改活动信息
     * @param activity
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateActivity(@RequestBody Activity activity) {
        Boolean flag = activityService.updateActivity(activity);
        if(flag){
            return Result.success();
        }else {
            return Result.error("修改活动失败");
        }
    }

    /**
     * 删除活动
     * @param activity
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteActivity(@RequestBody Activity activity) {
        Boolean flag = activityService.deleteActivity(activity);
        if(flag){
            return Result.success();
        }else {
            return Result.error("删除活动失败");
        }
    }

    /**
     * 分页查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = activityService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = activityService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }

    }

}
